package com.baomidou.springwind.excel;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.entity.CityOrder;
import com.baomidou.springwind.entity.NetworkNode;
import com.baomidou.springwind.service.CityOrderService;
import com.baomidou.springwind.service.NetworkNodeService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author luox
 */
@Component
public class CityOrderDataReader extends ExcelReader<CityOrder> {

    @Autowired
    private CityOrderService cityOrderService;
    @Autowired
    private NetworkNodeService networkNodeService;

    @Override
    protected String saveData(List<CityOrder> dataList, Long batchId) {
        //保存上传数据
        EntityWrapper<CityOrder> wrapper = new EntityWrapper<CityOrder>();
        wrapper.eq("batch_id", batchId);
        deleteFile(batchId);
        cityOrderService.delete(wrapper);//删除之前导入的数据
        for (CityOrder cityOrder : dataList) {
            cityOrder.setBatchId(batchId);
            cityOrder.setCreateTime(new Date());
            cityOrder.setOperatorId(getCurrentUserId());
        }
        cityOrderService.insertBatch(dataList, 100);
        //处理glc_network_node表数据
        EntityWrapper<NetworkNode> wrapper1 = new EntityWrapper<NetworkNode>();
        wrapper1.eq("batch_id", batchId);
        networkNodeService.delete(wrapper1);//删除之前导入的数据
        List<NetworkNode> networkNodes=orderToNetwork(dataList,batchId);
        networkNodeService.insertBatch(networkNodes, 100);
        return "";
    }

    /**
     * 城市配送订单CityOrder转为networkNote
     * @param dataList
     * @param batchId
     * @return
     */
    public List<NetworkNode> orderToNetwork(List<CityOrder> dataList,Long batchId){
        List<NetworkNode> networkNodes=new ArrayList<NetworkNode>();
        int type3=3;
        int type2=2;
        int count=0;
        for (int i=0;i<dataList.size();i++){
            NetworkNode networkNode = new NetworkNode();
            networkNode.setBatchId(batchId);
            networkNode.setNodeName(dataList.get(i).getName());
            networkNode.setNodeType(type3);
            networkNode.setCoordinateX(BigDecimal.valueOf(Double.valueOf(dataList.get(i).getLng1())));
            networkNode.setCoordinateY(BigDecimal.valueOf(Double.valueOf(dataList.get(i).getLat1())));
            networkNode.setAmount(BigDecimal.valueOf(Double.valueOf(dataList.get(i).getOrderPositive())));
            networkNode.setOperatorId(getCurrentUserId());
            networkNode.setCreateTime(new Date());
            networkNodes.add(networkNode);
            count=count+dataList.get(i).getOrderPositive();
        }
        if (dataList.size()>0){
            NetworkNode networkNode = new NetworkNode();
            networkNode.setBatchId(batchId);
            networkNode.setNodeName(dataList.get(0).getDistributionCenter());
            networkNode.setNodeType(type2);
            networkNode.setCoordinateX(BigDecimal.valueOf(Double.valueOf(dataList.get(0).getLng1())));
            networkNode.setCoordinateY(BigDecimal.valueOf(Double.valueOf(dataList.get(0).getLat1())));
            networkNode.setAmount(BigDecimal.valueOf(Double.valueOf(count)));
            networkNode.setOperatorId(getCurrentUserId());
            networkNode.setCreateTime(new Date());
            networkNodes.add(networkNode);
        }
        return networkNodes;
    }

    private void deleteFile(Long batchId) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String path = request.getSession().getServletContext().getRealPath("");
        path = path.substring(0, path.lastIndexOf("\\"));
        path = path.substring(0, path.lastIndexOf("\\"));
        File file = new File(path + "/temp/route/" + batchId + ".obj");
        file.delete();
    }

    @Override
    protected List<CityOrder> verifyData(HSSFWorkbook workbook) {
        List<CityOrder> cityOrderList = new ArrayList<CityOrder>();
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        CityOrder CityOrder;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            CityOrder = new CityOrder();
            CityOrder.setName(getStringCellValue(sheet.getRow(i), 0));
            CityOrder.setOrderPositive(Integer.valueOf(getStringCellValue(sheet.getRow(i), 1)));
            CityOrder.setLng1(getStringCellValue(sheet.getRow(i), 2));
            CityOrder.setLat1(getStringCellValue(sheet.getRow(i), 3));
            if(getStringCellValue(sheet.getRow(i), 4).length()!=0){
                CityOrder.setType(Integer.valueOf(getStringCellValue(sheet.getRow(i), 4)));
            }
            CityOrder.setDistributionCenter(getStringCellValue(sheet.getRow(i), 5));
            CityOrder.setLng2(getStringCellValue(sheet.getRow(i), 6));
            CityOrder.setLat2(getStringCellValue(sheet.getRow(i), 7));
            CityOrder.setFactory(getStringCellValue(sheet.getRow(i), 8));
            if(getStringCellValue(sheet.getRow(i), 9).length()!=0){
                CityOrder.setOrderRecovery(Integer.valueOf(getStringCellValue(sheet.getRow(i), 9)));
            }
            cityOrderList.add(CityOrder);
        }
        return cityOrderList;
    }

    @Override
    protected List<CityOrder> verifyData7(XSSFWorkbook workbook) {
        return null;
    }

}
